<template>
  <div>
    <Navbar :title="title" />
    <div class="detail-container">
      <!-- 轮播 -->
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="banner in pics" :key="banner.id">
          <img :src="banner.pic" alt="" />
        </van-swipe-item>
      </van-swipe>
      <!-- /轮播 -->
      <h2>
        {{ basicInfo.name }}
      </h2>
      <div class="content" v-html="content">
      </div>
    </div>
    <!-- 商品行动栏 -->
    <van-action-bar>
      <van-action-bar-icon @click="router.push('/home')" icon="wap-home-o" text="首页"  />
      <van-action-bar-icon @click="router.push('/cart')" icon="cart-o" text="购物车" badge="5" />
      <van-action-bar-button @click="showSkuAction('addCart')" type="warning" text="加入购物车" />
      <van-action-bar-button @click="showSkuAction('confirmOrder')" type="danger" text="立即购买" />
    </van-action-bar>
    <!-- 商品行动栏 -->
    <!-- 商品规格弹窗 -->
    <van-popup
      v-model:show="showSku"
      position="bottom"
      closeable
      round
      :style="{ height: '30%',padding: '10px' }"
    >
      <div>
        <van-card
          :num="sku.number"
          :price="sku.minPrice"
          :title="sku.name"
          :thumb="sku.pic"
        >
          <template #footer>
            <van-stepper v-model="sku.number" :min="1"/>
            <van-button size="large" @click="clickConfirmBtn" round type="danger">确定</van-button>
          </template>
        </van-card>
      </div>
    </van-popup>
    <!-- /商品规格弹窗 -->
  </div>
</template>

<script setup lang="ts" name="DetailPage">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router';
import Navbar from '@/components/Navbar.vue';
import { fetchItemDetail } from '@a/index'
const route = useRoute();
const router = useRouter();
// 商品详情数据
const basicInfo = ref<{ name: string, minPrice: number }>({})
const pics = ref<{ id: number, pic: string }[]>([])
const content = ref('')
// 商品规格
const sku = ref<{number: number, minPrice: number,name: string, pic: string}>({})
// 请求商品详情数据
const getDetail = () => {
  fetchItemDetail({
    id: route.query.id
  }).then(res => {
    if (res.data.code === 0) {
      basicInfo.value = res.data.data.basicInfo;
      pics.value = res.data.data.pics;
      content.value = res.data.data.content.replace(/<img/gi, "<img style='display:block;width: 100%'")
      sku.value = {
        ...res.data.data.basicInfo,
        number: 1
      }
    }

  })
}

// 商品规格弹窗
const showSku = ref(false)
let clickAction = 'addCart'; // 点击商品规格行为
const showSkuAction = (actionMsg: string) => {
  showSku.value = true
  clickAction = actionMsg
}
// 点击商品规格弹窗中的确定按钮
const clickConfirmBtn = () => {
  if (clickAction === 'addCart') {
    alert('加入购物车')
  } else {
    alert('去订单页')
  }
  showSku.value = false
}
onMounted(() => {
  getDetail()
})
</script>

<style scoped lang="scss">
.detail-container {
  padding-top: var(--van-nav-bar-height)
}

.my-swipe {
  width: 10rem;
  height: 5rem;

  img {
    width: 10rem;
    height: 5rem;
  }
}
:deep .van-popup{
  background: var(--van-card-background);
}
// .content{
//   :deep img{
//     display: block;
//     width: 100%;
//   }
// }
</style>